package com.atguigu.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * 统计发送成功或失败的消息数
 */
public class CountInterCeptor implements ProducerInterceptor<String, String> {


    private int success = 0;
    private int faild = 0;

    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {
        System.out.println("==========CountInterCeptor ===========");
        return producerRecord;
    }

    @Override
    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
        if(e != null){
            faild ++;
        }else {
            success ++ ;
        }

    }

    /**
     * close表示整个拦截器是执行结束了
     */
    @Override
    public void close() {
        System.out.println("SUCCESS:" + success);
        System.out.println("Faild:" + faild);

    }

    @Override
    public void configure(Map<String, ?> map) {



    }
}
